import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {MockConfigItem} from '../../entity/mock-config-item';
import {DocService} from '../../service/doc.service';
import {MockService} from '../../service/mock.service';
import {MockUtils} from '../../utils/mock-utils';
import {deepCopy} from '../../utils/entity-utils';

@Component({
    selector: 'app-doc-mock-rest-config',
    templateUrl: './doc-mock-rest-config.component.html',
    styleUrls: ['./doc-mock-rest-config.component.css']
})
export class DocMockRestConfigComponent implements OnInit {
    @Input()
    loopedEntityMap: Map<string, MockConfigItem>;

    @Input()
    mockConfigItem: MockConfigItem;

    @Input()
    level = 0;

    @Input()
    isLast = true;

    @Input()
    mapIndex = -1;

    @Output()
    mapItemDeleteAction = new EventEmitter();
    /**
     * 仅在pojo类型上有用
     */
    @Input()
    showAvailableCheckbox = false;

    /**
     * map key
     */
    mapKey: string;

    constructor(
        private docService: DocService,
        private mockService: MockService
    ) {
    }

    ngOnInit(): void {
        // if (this.mockConfigItem.type === 4) {
        //     console.log(this.mockConfigItem);
        // }
    }

    activeMockConfigItem(item: MockConfigItem) {
        this.mockService.activeMockConfigItem(item);
    }

    getMockConfigText(mockConfigItem: MockConfigItem, maxLen?: number): string {
        // 转换成数值，否则switch无法匹配上
        if (mockConfigItem.type === 3) {
            // collection
            return MockUtils.getCollectionTypeMockConfigDisplay(mockConfigItem);
        } else {
            const text = MockUtils.getBaseTypeMockConfigDisplay(mockConfigItem);
            if (text && mockConfigItem.baseType === 1 && maxLen && maxLen > 0) {
                if (text.length > maxLen) {
                    return text.substring(0, maxLen) + '...';
                }
            }
            return text;
        }
    }

    recoverEntity(entityName: string) {
        if (!this.mockConfigItem.loaded) {
            return;
        }
        const item = this.loopedEntityMap.get(entityName);
        if (!item) {
            return;
        }
        // this.mockConfigItem.loaded = false;
        item.children.forEach(child => {
            const dto = deepCopy(child);
            this.mockConfigItem.children.push(dto);
        });
    }

    hideEntity(entityName: string) {
        if (!this.mockConfigItem.loaded) {
            return;
        }
        this.mockConfigItem.children.splice(0, this.mockConfigItem.children.length);
    }

    delMapItem(mapIndex: number) {
        this.mapItemDeleteAction.emit(mapIndex);
    }

    deleteMap(mapItemIndex?: number) {
        if (mapItemIndex === null) {
            return;
        }
        this.mockConfigItem.children.splice(mapItemIndex, 1);
    }

    editMapKey() {
        this.mapKey = this.mockConfigItem.key;
        this.mockConfigItem.edit = true;
    }

    exitMapKey() {
        this.mockConfigItem.edit = false;
    }

    saveMapKey() {
        if (!this.mapKey) {
            return;
        }
        this.mockConfigItem.key = this.mapKey;
        this.mockConfigItem.edit = false;
    }
}
